<?php

/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2017/6/28
 * Time: 10:46
 */

namespace Admin\Controller;

use Admin\Service\FinanceService;

class FinanceController extends BaseController {


    public function __construct() {
        parent::__construct();
        $this->financeService = new FinanceService();
    }

    public function log() {
        $this->display();
    }
    public function ajax_log() {

        $nowPage = I('get.p', 1);
        $username = I('post.username', '');
        $mobile = I('post.mobile', '');

        $list = $this->financeService->logList($username,$mobile,4,1);
        $page = $list['data'];

        $busConductList = $this->financeService->logList($username, $mobile,4,0,$nowPage);

        $this->assign('page', $page->show());
        $this->assign('lists', $busConductList['data']);
        $this->display();
    }


    public function export()
    {
        $selected = array_filter(I('post.selected'));
        if(empty($selected)){
            return $this->error('请选择导出的条目');
        }

        $strTable = '<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">ID</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">会计师名称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">会计师手机</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">类型</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">提现状态</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">提现时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">生成时间</td>';
        $strTable .= '</tr>';

        $obj = M('pay_log')->alias('al')->field('al.*,au.username,au.mobile')
            ->join(' LEFT JOIN ' . C('DB_PREFIX') . 'accountant_users AS au ON au.au_id = al.au_id')
           ->where('id in ('.implode(',',$selected).')')
            ->order('al.id desc')->select();

        foreach($obj as $val) {
            $strTable .= '<tr>';
            $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['id'] . ' </td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['username'] . ' </td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['mobile'] . ' </td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">提现 </td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['money'] . '  </td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">' . (($val['status'] == 1) ? '成功':'<font color="red">失败</font>' ). '  </td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">' . date('Y-m-d H:i:s',$val['pay_time']) . ' </td>';
            $strTable .= '<td style="text-align:left;font-size:12px;">' . date('Y-m-d H:i:s',$val['add_time']) . ' </td>';
            $strTable .= '</tr>';
        }
        $strTable .= '</table>';

        downloadExcel($strTable, '会计师提现表');
        exit();
    }


    public function detail() {
        $mobile =  I('get.mobile');
        $this->assign('type', C('ACCOUNTING_LOG_TYPE'));
        $this->assign('mobile', $mobile);
        $this->display();
    }
    public function ajax_detail() {

        $nowPage = I('get.p', 1);
        $username = I('post.username', '');
        $mobile = I('post.mobile') ;
        $type = I('post.type', '');

        $list = $this->financeService->logList($username,$mobile,$type,1);
        $page = $list['data'];

        $busConductList = $this->financeService->logList($username, $mobile,$type,0,$nowPage);

        $this->assign('type', C('ACCOUNTING_LOG_TYPE'));
        $this->assign('page', $page->show());
        $this->assign('lists', $busConductList['data']);
        $this->display();
    }


    public function trade() {
        $this->assign('type', C('ACCOUNTING_TRADE_TYPE'));
        $this->display();
    }

    public function ajax_trade() {

        $nowPage = I('get.p', 1);
        $username = I('post.username', '');
        $mobile = I('post.mobile') ;
        $type = I('post.type', '');

        $list = $this->financeService->tradeList($username,$mobile,$type,1);
        $page = $list['data'];

        $busConductList = $this->financeService->tradeList($username, $mobile,$type,0,$nowPage);

        $this->assign('type', C('ACCOUNTING_TRADE_TYPE'));
        $this->assign('method', C('ACCOUNTING_TRADE_METHOD'));
        $this->assign('page', $page->show());
        $this->assign('lists', $busConductList['data']);
        $this->display();
    }


}
